<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
    <meta http-equiv="X-UA-Compatible" content="ie=edge,chrome=1">
    <meta name="referrer" content="never">
    <title>获取对象所有属性的方法</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        ul {
            list-style: none;
        }
    </style>
</head>
<body>
    
    <script type="text/javascript">
        /* 
        对象的数据属性标识(writable/value)与访问器属性标识(get/set)不能共存

        Object.keys(obj) 获取所有可遍历属性，得到一个数组

        Object.getOwnPropertyNames(obj)  不论属性是否可遍历，都输出，得到一个数组
        */    

        var user = {
            name: "李白",
            age: 34,
            tel: "32452345",
            address: "长安"
        }

        Object.defineProperties(user, {
            height: {
                value: 170
            },
            weight: {
                get(){
                    return 130
                }
            }
        })

        console.log("Object.keys(user)", Object.keys(user));
        console.log("Object.getOwnPropertyNames(user)", Object.getOwnPropertyNames(user));


        // Object.getOwnpropertyDescriptor(obj, "属性名") 查看对象的属性描述符，默认获取数据属性，如果属性设置的是访问器属性，获取的就是访问器属性
        console.log(Object.getOwnPropertyDescriptor(user, "name"));
        console.log(Object.getOwnPropertyDescriptor(user, "height"));
        console.log(Object.getOwnPropertyDescriptor(user, "weight"));
    </script>
</body>
</html>